//
//  CommunityViewController.m
//  RedManStock
//
//  Created by lanou3g on 15/7/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CommunityViewController.h"
#import "CommunityListTableViewCell.h"
#import "MyHandle.h"
#import "CommunityList.h"
#import "CommunityDetailViewController.h"
@interface CommunityViewController ()
//数据源
@property(nonatomic,retain)NSMutableArray *communityArray;

@end

@implementation CommunityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _communityArray = [NSMutableArray array];
    
    self.update=YES;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CommunityListTableViewCell" bundle:nil] forCellReuseIdentifier:@"communityCell"];
    
    
}

//http://api.gongniucaijing.com/Weiba/listweiba?token=d58f02522dac7e698a4600c8b18ff02c0b9fa6df&number=15&offset=15

- (void)viewWillAppear:(BOOL)animated{
    
    if (self.update) {
        [self loadNewData];
    }
    self.update=NO;
}

#pragma mark- 下拉刷新（请求数据，结束header） -
-(void)loadNewData{
    NSString *urlStr = @"http://api.gongniucaijing.com/Weiba/listweiba";
    [self.communityArray removeAllObjects];
    [[MyHandle new] requestDataWithURLStr:urlStr parameterBlock:^NSDictionary *{
        NSDictionary *dic = @{@"token":@"d58f02522dac7e698a4600c8b18ff02c0b9fa6df",
                              @"number":@"15",
                              @"offset":@"15"};
        return dic;
        
    } successBlock:^(NSDictionary *responseDic) {
        [self dataSourceWithJasonDic:responseDic];
        [self.tableView.header endRefreshing];
        
    } errorBlock:^(NSError *error) {
        
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alertView show];
        [self.tableView.header endRefreshing];

        
    }];
    
    
}

#pragma mark- 上拉加载方法（请求数据，结束footer） -
-(void)loadMoreData{
    
    NSString *urlStr = @"http://api.gongniucaijing.com/Weiba/listweiba";
    [[MyHandle new] requestDataWithURLStr:urlStr parameterBlock:^NSDictionary *{
        NSDictionary *dic = @{@"token":@"d58f02522dac7e698a4600c8b18ff02c0b9fa6df",
                              @"number":@"15",
                              @"offset":[NSString stringWithFormat:@"%lu",(unsigned long)self.communityArray.count]};
        return dic;
        
    } successBlock:^(NSDictionary *responseDic) {
        [self dataSourceWithJasonDic:responseDic];
        [self.tableView.header endRefreshing];
        
    } errorBlock:^(NSError *error) {
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        
        [alertView show];
        [self.tableView.header endRefreshing];
       
    }];
    
}

#pragma mark- 共用方法：数据源赋值，reloadData -
- (void)dataSourceWithJasonDic:(NSDictionary *)dic{
    
    NSDictionary *dataDic = [dic objectForKey:@"data"];
    NSDictionary *weibaDic = [dataDic objectForKey:@"weiba"];
    NSMutableArray *commnityArray = [weibaDic objectForKey:@"lists"];
    for (NSDictionary *dic in commnityArray) {
        
        CommunityList *community = [[CommunityList alloc]init];
//        给模型赋值
        [community setValuesForKeysWithDictionary:dic];
//        把模型加入数据源
        [self.communityArray addObject:community];
        
    }
    
    [self.tableView reloadData];
    
}




#pragma mark- 协议方法：分行 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.communityArray.count;
}

#pragma mark- 分区 -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

#pragma mark- 协议方法cell复用 -
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    从数据源中取出模型
    
    CommunityListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"communityCell" forIndexPath:indexPath];
CommunityList *community = [self.communityArray objectAtIndex:indexPath.row];
    
    cell.community = community;
    
    if (!community.weiba_logoImage ) {
        [MyHandle new];
        [cell.weiba_logoImageView sd_setImageWithURL:[NSURL URLWithString:community.weiba_logo] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            community.weiba_logoImage = cell.weiba_logoImageView.image;
            
        }];
        
    }
    
    cell.weiba_logoImageView.image = community.weiba_logoImage;
    
    
    
    return cell;
    
}

#pragma mark- cell高度 -
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 85;
}

#pragma mark- cell被选中方法 -
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    在数组中取出模型，用于传值(属性传值)
   CommunityList *community = [self.communityArray objectAtIndex:indexPath.row];
    CommunityDetailViewController *CommunityDetailVC = [[CommunityDetailViewController alloc]initWithHeader:YES footer:YES];
    
    CommunityDetailVC.community =community;
    
    [self.navigationController pushViewController:CommunityDetailVC animated:YES];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
